$(function() {
	$.ajax({
		url : __ctx + "/card/department/departmentImport/action_clear.htm",
		type : "post",
		async : false,
		success : function(data) {
			refoundExcelEntity = new RefoundExcelEntity();
			refoundExcelEntity.init();
			formUrl = refoundExcelEntity.formUrl;
		}
	});
});
(function() {
	// 定义常量
	var _consts = {
		GRID : "#refoundImportGrid",// 列表对象
		PAGER : "#refoundImportPager",// 列表分页
		FORM : '#refoundImportForm',// 表单form
	// FORMGET : '#cardPidFormGet'// 表单form

	};
	RefoundExcelEntity = function() {
		// 定义属性
	};
	RefoundExcelEntity.prototype = {
		consts : _consts,
		init : function() {
			if (this.hasInit) // 是否已初始化
				return false;
			this.hasInit = true;
			if ($(this.consts.GRID).length > 0) { // 列表
				this._initGridList();
				this._initEvent();
			}
			if ($(this.consts.FORM).length > 0) { // 表单
				this._initForm();
				this._initData();
				this._initOffice('e');
			}
			if ($(this.consts.FORMGET).length > 0) { // 明细页面office控件初始化
				this._initOffice('r');
			}
		},
		_initOffice : function(_rights) {
		},

		_initGridList : function() {
			var me = this;
			var id = $("#id").val();
			$(this.consts.GRID).GridList({
				url : __ctx + '/card/department/departmentImport/listPage.htm',
				datatype : "json",
				pager : this.consts.PAGER,
				colModel : [ {
					name : 'academy',
					index : 'academy',
					label : '学院',
					width : 100
				}, {
					name : 'major',
					index : 'major',
					label : '专业',
					width : 100
				}, 
				{
					name : 'grade',
					index : 'grade',
					label : '年级',
					width : 80
				},{
					name : 'classname',
					index : 'classname',
					label : '班级',
					width : 80
				}, {
					name : 'remark',
					index : 'remark',
					label : '提示',
					width : 150
				} ]
			});
		},
		_initEvent : function() {
			var me = this;
			$("#import").on("click", function() {
				$("#file").click();
			});
			$("#file").on("change", function() {
				$("#photo1").val($("#file").val());
			});
			$("#export").on("click", function() {
				var rows = $(_consts.GRID).jqGrid('getRowData');
				if ($.isEmpty(rows))
					return;
				location.href = __ctx + "/card/department/departmentImport/action_export.htm";
			});
			$(document).on("click", "a.fa-save", function() {
				var fileObj = document.getElementById("file").files[0];
				if (fileObj == undefined) {
					DialogUtil.toastr("文件格式不对，请选择xls或xlsx格式！");
					return;
				}
				var filename = fileObj.name;
				var ex = filename.lastIndexOf(".");
				var suffix = filename.substring(ex).toLowerCase();
				if (!(".xlsx" == suffix || ".xls" == suffix)) {
					DialogUtil.alert("文件格式不对，请选择xls或xlsx格式！");
					return;
				}
				var data = new FormData();
				var fileObj = document.getElementById("file").files[0];
				data.append("file", fileObj);
				var lid = null;
				$.ajax({
					url : __ctx + "/card/department/departmentImport/action_import.htm",
					data : data,
					type : "post",
					dataType : "json",
					cache : false, // 上传文件无需缓存
					processData : false, // 用于对data参数进行序列化处理 这里必须false
					contentType : false, // 必须
					success : function(data) {
						if (data.result == 1) {
							if (data.rows > 0) {
								DialogUtil.alert("成功导入 <b>" + data.rows + "</b> 条数据");
							} else {
								DialogUtil.warn("未导入任何数据");
							}
							$(_consts.GRID).trigger("reloadGrid");
						} else {
							DialogUtil.error(data.message);
						}
					},
					error : function(data) {
						DialogUtil.alert(data.message);
					},
					beforeSend : function() {
						lid = DialogUtil.load();
					},
					complete : function() {
						if (lid != null)
							DialogUtil.close(lid);
					}
				});
			});
		},
		/**
		 * 初始化表单
		 */
		_initForm : function() {
			var me = this, form = $(this.consts.FORM), frm = form.form();
			me.formUrl = new com.lc.form.FormData(form);
			// 触发表单验证
			frm.valid();
			// 处理表单保存
			$(document).on('click', 'a.fa-save', function() {
				var $el = $(this);
				$el.button('loading');
				// office提交
				OfficePlugin.submit();
				me.formUrl.submit(me._showResponse, $el);
			});
		},
		/**
		 * 初始化数据
		 */
		_initData : function() {
			if (!$.isEmpty(frameElement) && !$.isEmpty(frameElement.dialog) && !$.isEmpty(frameElement.dialog.params) && !$.isEmpty(frameElement.dialog.params.data)) {
				var data = frameElement.dialog.params.data;
				this.formUrl.setData("[name^='m:']", data);
				AttachementControl.initDataTo($("div[name='div_attachment_container']")); // 附件初始化
			} else {
				$("[name^='m:']", $(_consts.FORM)).each(function() {
					var data = $(this);
					// 单选框特殊处理
					if (data.is('input') && "radio" == data.attr("type")) {
						var defaultVal = data.attr("defaultVal");
						var defaultValue = data.attr("defaultValue");
						if (defaultValue == "true" && defaultVal == "true") {
							data.prop("checked", "checked");
						}
					}
				});
			}

			this.formUrl.validate();
		},
	};
})()
